/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

var osapps = osapps || {};
osapps.req = osapps.req || {};

osapps.req.Request_DefaultUrl = "req";
osapps.req.Request_DefaultHttpParameters = {};
osapps.req.Request_DefaultRequestSettings = {};
osapps.req.Request_DefaultRequestSettings[gadgets.io.RequestParameters.CONTENT_TYPE] = gadgets.io.ContentType.JSON;
osapps.req.Request_DefaultRequestSettings[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;

osapps.req.Request = {
  url : osapps.req.Request_DefaultUrl,
  httpParameters : null,
  callback : null,
  requestSettings : null,
  
  debug : true,
  
  //should be called in constructor of child class.
  initDefault : function() {
    this.httpParameters = this.httpParameters || {};
    osapps.mixin(this.httpParameters, osapps.req.Request_DefaultHttpParameters);

    this.requestSettings = this.requestSettings || {};
    osapps.mixin(this.requestSettings, osapps.req.Request_DefaultRequestSettings);
  },
  
  checkExist : function (parameter) {
    if (this.httpParameters) {
      return (parameter in this.httpParameters) 
             && (this.httpParameters[parameter] !== undefined) 
             && (this.httpParameters[parameter] !== null)
             && (("" + this.httpParameters[parameter]).trim().length > 0)
             && (!(this.httpParameters[parameter] instanceof Array)
                || this.httpParameters[parameter].length > 0);
    }
    return false;
  },
  checkValue : function (parameter, value) {
    if (this.httpParameters) {
      return (parameter in this.httpParameters) 
             && (this.httpParameters[parameter] === value);
    }
    return false;
  },
  put : function (parameter, value) {
    this.httpParameters = this.httpParameters || {};
    
    value = value == null ? "" : value;
    this.httpParameters[parameter] = value;    
  },
  set : function (parameter, value) {
    this.httpParameters = this.httpParameters || {};
    
    value = value == null ? "" : value;
    this.httpParameters.put(parameter, value);    
  },
  
  send : function(opt_callback) {
    if (!this.url){
      alert("Property 'url' should be set.");
      return;
    }
    var callback = opt_callback || this.callback;
    if (!callback){
      alert("Property 'callback' should be set.");
      return;
    }
    if (this.isValid && !this.isValid()) {
      alert("Request object is invalid.");
      return;
    }
    if(this.requestSettings && this.requestSettings[gadgets.io.RequestParameters.METHOD] == gadgets.io.MethodType.POST){
      var postdata = gadgets.io.encodeValues(this.httpParameters);
      this.requestSettings[gadgets.io.RequestParameters.POST_DATA] = postdata;
      
      var url = osapps.Loader.deployInfo.backend_url + this.url;
      gadgets.io.makeRequest(url, callback, this.requestSettings);
      return true;
    } else {
      var params = "";
      for (var parameter in this.httpParameters) {
        var value = this.httpParameters[parameter];
        if (value == null)  value = "";
        if (value instanceof Array) {
          for (var i = 0; i < value.length; i++) {
            var v = value[i];
            if (v == null) v = "";
            if (params != "") params += "&";
            params += parameter + "=" + encodeURIComponent(v);
          }
        } else {
          if (params != "") params += "&";
          params += parameter + "=" + encodeURIComponent(value);
        }
      }
      if (params != "") params = "?" + params;
      var url = osapps.Loader.deployInfo.backend_url + this.url + params;
      
	  if (this.debug) {
                var ts = new Date().getTime();
                var sep = "?";
                ts = Math.floor(ts / 1000);
                if (url.indexOf("?") > -1) {
                    sep = "&";
                }
                url = [url, sep, "nocache=", ts].join("");
                
            }
	  gadgets.io.makeRequest(url, callback, this.requestSettings);
      return true;
    }
  }
};

osapps.req.GetRequest_DefaultHttpParameters = {};
osapps.req.GetRequest_DefaultRequestSettings = {};
osapps.req.GetRequest_DefaultRequestSettings[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;

osapps.req.GetRequest = {};
osapps.mixin(osapps.req.GetRequest, osapps.req.Request);

osapps.req.GetRequest.initDefault = function() {
  osapps.req.Request.initDefault.call(this);
  osapps.mixin(this.httpParameters, osapps.req.GetRequest_DefaultHttpParameters);
  osapps.mixin(this.requestSettings, osapps.req.GetRequest_DefaultRequestSettings);
};
 
osapps.req.GetRequest.send = function(opt_callback) {
  this.requestSettings[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.GET;
  osapps.req.Request.send.call(this, opt_callback);
};

osapps.req.extendsGetRequest = function(obj) {
  osapps.mixin(obj, osapps.req.GetRequest);
  osapps.req.GetRequest.initDefault.call(obj);
};

osapps.req.PostRequest_DefaultHttpParameters = {};
osapps.req.PostRequest_DefaultRequestSettings = {};
osapps.req.PostRequest_DefaultRequestSettings[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;

osapps.req.PostRequest = {};
osapps.mixin(osapps.req.PostRequest, osapps.req.Request);

osapps.req.PostRequest.initDefault = function() {
  osapps.req.Request.initDefault.call(this);
  osapps.mixin(this.httpParameters, osapps.req.PostRequest_DefaultHttpParameters);
  osapps.mixin(this.requestSettings, osapps.req.PostRequest_DefaultRequestSettings);
};
 
osapps.req.PostRequest.send = function(opt_callback) {
  this.requestSettings[gadgets.io.RequestParameters.METHOD] = gadgets.io.MethodType.POST;
  osapps.req.Request.send.call(this, opt_callback);
};

osapps.req.extendsPostRequest = function(obj) {
  osapps.mixin(obj, osapps.req.PostRequest);
  osapps.req.PostRequest.initDefault.call(obj);
};